package com.jake.xfeed.recyclerfeed;

import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class SingleFooterFeedAdapter<FooterView extends View> extends RecyclerView.Adapter<SingleFooterFeedAdapter.ViewHolder> {
    @NonNull
    private final FooterView mFooterView;
    private Observer<FooterView> mObserver;

    public SingleFooterFeedAdapter(@NonNull FooterView footerView) {
        mFooterView = footerView;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(mFooterView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if (mObserver != null) {
            mObserver.onBindViewHolder(mFooterView);
        }
    }

    public void setObserver(Observer<FooterView> mObserver) {
        this.mObserver = mObserver;
    }

    @Override
    public void onViewAttachedToWindow(@NonNull ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        if (mObserver != null) {
            mObserver.onViewAttachedToWindow(mFooterView);
        }
    }

    @Override
    public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        if (mObserver != null) {
            mObserver.onDetachedFromRecyclerView(mFooterView);
        }
    }

    @Override
    public void onViewRecycled(@NonNull ViewHolder holder) {
        super.onViewRecycled(holder);
        if (mObserver != null) {
            mObserver.onViewRecycled(mFooterView);
        }
    }

    @Override
    public int getItemCount() {
        return 1;
    }

    public interface Observer<FooterView> {
        void onBindViewHolder(@NonNull FooterView footerView);

        void onViewAttachedToWindow(@NonNull FooterView footerView);

        void onDetachedFromRecyclerView(@NonNull FooterView footerView);

        void onViewRecycled(@NonNull FooterView footerView);
    }

    public abstract static class ObserverAdapter<FooterView> implements Observer<FooterView> {

        @Override
        public void onBindViewHolder(@NonNull FooterView footerView) {

        }

        @Override
        public void onViewAttachedToWindow(@NonNull FooterView footerView) {

        }

        @Override
        public void onDetachedFromRecyclerView(@NonNull FooterView footerView) {

        }

        @Override
        public void onViewRecycled(@NonNull FooterView footerView) {

        }
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
